package models;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import objects.Feature;
import objects.FeatureList;
import objects.Form;
import objects.Lexicon;
import objects.LexiconList;
import objects.Stem;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/* Accessible only through LexiconModel */
class LexiconXMLWriter {
	
	private Lexicon db;
	
	public LexiconXMLWriter() {
		db = null;
	}
	
	public void write(File file) throws FileNotFoundException, IOException {
		Element theRoot = putXML();

		Document doc = new Document(theRoot);
		XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
		xmlOutput.output(doc, new FileOutputStream(file));
	}
	
	public Element putXML() {
		Element theRoot = new Element("lexicon");

		if(db.getLexiconList() != null) {
			for (LexiconList lexiconList : db.getLexiconList()) {
				Element lexicon = new Element("lexiconList");
				
				lexicon.setAttribute("label", lexiconList.getLabel());
				
					Element formList = new Element("formList");
					
					for (String currentValue : lexiconList.getFormList()) {
						Element value = new Element("value");
						value.setText(currentValue);
						formList.addContent(value);
					}
							
					lexicon.addContent(formList);
				
					for (FeatureList currentFeature : lexiconList.getFeatureList()) {
						Element feature = new Element("featureList");
						
						feature.setAttribute("name", currentFeature.getName());
					
						for (String currentValue : currentFeature.getValues()) {
							Element value = new Element("value");
							value.setText(currentValue);
							feature.addContent(value);
						}
						
						lexicon.addContent(feature);
					}
					
					for(Stem currentStem : lexiconList.getStemList()) {
						Element stem = new Element("stemList");
						
						stem.setAttribute("value", currentStem.getValue());
						stem.setAttribute("concept", currentStem.getConcept());
						stem.setAttribute("sense", currentStem.getSense());
						
							Element feature = new Element("featureList");
	
							for (Feature currentFeature : currentStem.getFeatureList()) {
								Element featureStem = new Element("feature");
								
								featureStem.setAttribute("name", currentFeature.getName());
								featureStem.setAttribute("value", currentFeature.getValue());
								feature.addContent(featureStem);
							}
		
							stem.addContent(feature);
							
							Element formListStem = new Element("formList");
							
							for (Form currentForm : currentStem.getFormList()) {
								Element formStem = new Element("form");
								
								formStem.setAttribute("name", currentForm.getName());
								formStem.setAttribute("value", currentForm.getValue());
								
								formListStem.addContent(formStem);
							}
							
							stem.addContent(formListStem);
						
						lexicon.addContent(stem);
					}

				theRoot.addContent(lexicon);
			}
		}
		
		return theRoot;
	}
	
	public void setDatabase(Lexicon db) {
		this.db = db;
	}
	
}
